import request from "@/utils/request";
import cache from "@/utils/cache";

export async function getDicts(dictType) {
  const key = dictType;
  let dict = cache.local.getJSON(key);
  if (dict) {
    return Promise.resolve(dict);
  }
  dict = await request({
    url: "/dict/list/" + dictType,
    method: "get",
  });
  cache.local.setJSON(key, dict);
  return Promise.resolve(dict);
}

/***	字典类型 */

export function listType(query){
  return request({
    url: "/dict/type/list",
    method: "get",
    params: query,
  });
}

export function getType(id){
  return request({
    url: "/dict/type/" + id,
    method: "get",
  });
}

export function saveOrUpdateType(data){
  return request({
    url: "/dict/type/saveOrUpdate",
    method: "post",
    data: data,
  });
}

export function removeType(id){
  return request({
    url: "/dict/type/delete/" + id,
    method: "delete",
  });
}

/*** 数据字典接口 */


export function listDict(query){
  return request({
    url: "/dict/list",
    method: "get",
    params: query,
  });
}

export function getDict(id){
  return request({
    url: "/dict/" + id,
    method: "get",
  });
}

export function saveOrUpdateDict(data){
  return request({
    url: "/dict/saveOrUpdate",
    method: "post",
    data: data,
  });
}

export function removeDict(id){
  return request({
    url: "/dict/delete/" + id,
    method: "delete",
  });
}


